package com.yochi.pictureseditor.activities;

import com.yochi.pictureseditor.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class ColorPicker extends Activity{
	
	public static final String TAG = "ColorPicker";

	private Intent resultIntent = new Intent();
	
	private TableRow tablerow_h;
	private TableRow tablerow_1;
	private TableRow tablerow_2;
	private TableRow tablerow_3;
	private TableRow tablerow_b;
	
	private TextView textViewHead;
	private SeekBar seekbar_red;
	private SeekBar seekbar_green;
	private SeekBar seekbar_blue;
	private Button okButton;
	private Button cancelButton;
	
	private int r;
	private int g;
	private int b;
	
    @Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(TAG,"::onCreate /Settings.java/");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_colorpicker);
     
        //get views
        tablerow_h = (TableRow) findViewById(R.id.tablerow_color_head);
    	tablerow_1 = (TableRow) findViewById(R.id.tablerow_color_1);
    	tablerow_2 = (TableRow) findViewById(R.id.tablerow_color_2);
    	tablerow_3 = (TableRow) findViewById(R.id.tablerow_color_3);
    	tablerow_b = (TableRow) findViewById(R.id.tablerow_buttons);
        textViewHead = (TextView) findViewById(R.id.textview_color);
		seekbar_red = (SeekBar) findViewById(R.id.seekbar_red);
		seekbar_green = (SeekBar) findViewById(R.id.seekbar_green);
		seekbar_blue = (SeekBar) findViewById(R.id.seekbar_blue);
    	okButton = (Button) findViewById(R.id.button_color_ok);
    	cancelButton = (Button) findViewById(R.id.button_color_cancel);
        
        // get extras
        Bundle bundle = getIntent().getExtras();
        if(bundle!=null) {
        	textViewHead.setText(bundle.getString("name"));
        	resultIntent.putExtra("effect", bundle.getSerializable("effect"));
        	seekbar_red.setMax(100);
        	seekbar_green.setMax(100);
        	seekbar_blue.setMax(100);
        }
        else textViewHead.setText(R.string.parameter_value);
        
        // red seekbar listener
        seekbar_red.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        	public void onStopTrackingTouch(SeekBar arg0) {}
        	public void onStartTrackingTouch(SeekBar arg0) {}
        	public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
        		r = progress;
        		refreshView();
        }	});
        
        // green seekbar listener
        seekbar_green.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        	public void onStopTrackingTouch(SeekBar arg0) {}
        	public void onStartTrackingTouch(SeekBar arg0) {}
        	public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
        		g = progress;
        		refreshView();
        }	});
        
        // blue seekbar listener
        seekbar_blue.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        	public void onStopTrackingTouch(SeekBar arg0) {}
        	public void onStartTrackingTouch(SeekBar arg0) {}
        	public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
        		b = progress;
        		refreshView();
        }	});
        
        // validate button listener
        okButton.setOnClickListener(new Button.OnClickListener() {  
	        public void onClick(View v) {
	        	resultIntent.putExtra("r", r);
	        	resultIntent.putExtra("g", g);
	        	resultIntent.putExtra("b", b);
	        	setResult(RESULT_OK, resultIntent);
	        	onDestroy();
	    }	});
        
        // cancel button listener
        cancelButton.setOnClickListener(new Button.OnClickListener() {  
	        public void onClick(View v) {
	        	setResult(RESULT_CANCELED, resultIntent);
	        	onDestroy();
	    }	});
	}
	
    /** Private methode to refesh view */
    private void refreshView(){
    	String sR;
    	String sG;
    	String sB;
		if(r<=15) sR = "0"+Integer.toHexString(r);
		else sR = Integer.toHexString(r);
		if(g<=15) sG = "0"+Integer.toHexString(g);
		else sG = Integer.toHexString(g);
		if(b<=15) sB = "0"+Integer.toHexString(b);
		else sB = Integer.toHexString(b);
    	long color_h = Long.parseLong("CC"+"222222", 16);
    	long color_b = Long.parseLong("EE"+sR+sG+sB, 16);
    	tablerow_h.setBackgroundColor((int) color_h);
    	tablerow_1.setBackgroundColor((int) color_b);
    	tablerow_2.setBackgroundColor((int) color_b);
    	tablerow_3.setBackgroundColor((int) color_b);
    	tablerow_b.setBackgroundColor((int) color_b);
    }
    
    @Override
    public void onDestroy() {
    	super.onDestroy();
    	finish();
    }
}
